package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.AfterSaleOutHouse;
import com.sl.au.entity.AfterSaleTicket;
import com.sl.au.repository.AfterSaleOutHouseRepository;
import com.sl.au.service.AfterSaleAssessService;
import com.sl.au.service.AfterSaleInHouseService;
import com.sl.au.service.AfterSaleTicketService;
import com.sl.au.service.OptionService;
 
/*
 * 售后评价
 */

@RestController
@RequestMapping(value = "/ui/afterSaleAssess")
public class AfterSaleAssessController extends BaseController {
	@Autowired
	AfterSaleAssessService afterSaleAssessService;

	@Autowired
	AfterSaleTicketService afterSaleTicketService;

	@Autowired
	OptionService optionService;

	@Autowired
	AfterSaleInHouseService afterSaleInHouseService;
	
	@Autowired
	AfterSaleOutHouseRepository afterSaleOutHouseRepository;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("afterSaleAssess/assessindex");
		return modelAndView;
	}

	@RequestMapping(value = "/getpage", method = RequestMethod.GET)
	public Map<String, Object> getAssessPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		String isSubmit=request.getParameter("isSubmit");
		Account a = this.getMyAccount();
		String userId = a.getUser().getId();
		List<AfterSaleTicket> afterSaleTickets = afterSaleInHouseService.getMyAfterSaleAccess(start, length, key,
				userId, isSubmit);
		long count = afterSaleInHouseService.getMyAfterSaleAccessCount(key, userId, isSubmit);
		map.put("data", afterSaleTickets);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "finishticket", method = RequestMethod.GET)
	public Map<String, Object> finishticket(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String id=request.getParameter("id");
		AfterSaleTicket a=afterSaleTicketService.getTicketRepository().findOne(id);
		List<AfterSaleOutHouse> outs=afterSaleOutHouseRepository.findAllOuts(id);
		for(AfterSaleOutHouse o:outs){
			if(o!=null){
				o.setOutState("4");
				afterSaleOutHouseRepository.save(o);
			}
		}
		a.setIsSubmit("6");
		afterSaleTicketService.getTicketRepository().save(a);
		map.put("code", 0);
		return map;
	}

}
